﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once

#include "ICommonEditTrimManager.h"
#include "UniIdentity.h"
#include "IElement.h"

namespace gcmp
{
    template<class ElementClass, class TrimStrategyClass>
    class ElementTrimStrategyRegister
    {
    public:
        ElementTrimStrategyRegister()
        {
            auto* pMgr = ICommonEditTrimManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pMgr, L"pMgr为空", L"GMEP", L"2020-12-22");

            const UniIdentity& implUid = ElementClass::GetImplementationUid();
            IElementTrimStrategy* pTrimStrategy = pMgr->GetStrategy(implUid);
            if (!pTrimStrategy)
            {
                OwnerPtr<IElementTrimStrategy> opTrimStrategy = NEW_AS_OWNER_PTR(TrimStrategyClass);
                pMgr->AddStrategy(implUid, TransferOwnership(opTrimStrategy));
            }
        }

        ~ElementTrimStrategyRegister() {}
    };
}

#define REGISTER_ELEMENT_TRIM_STRATEGY(ELEMENT_CLASS, TRIM_STRATEGY_CLASS) \
namespace { \
    static gcmp::ElementTrimStrategyRegister<ELEMENT_CLASS, TRIM_STRATEGY_CLASS> \
        s_##ELEMENT_CLASS##TRIM_STRATEGY_CLASS##ElementTrimStrategyRegister; \
    }
